/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.in;

import wtkx.Application;
import wtkx.Component;
import wtkx.Scene;

import jbxml.Dictionary;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowEvent;
import java.net.URL;

/**
 * Application drawable. 
 * 
 * @see Awtk
 * @see BackingStore
 * @see Screen
 * @author jdp
 */
public class Canvas 
    extends java.awt.Canvas 
    implements BackingStore.Component
{
    private final static long EVENT_MASK =  (AWTEvent.COMPONENT_EVENT_MASK
                                           | AWTEvent.FOCUS_EVENT_MASK
                                           | AWTEvent.MOUSE_EVENT_MASK
                                           | AWTEvent.MOUSE_MOTION_EVENT_MASK
                                           | AWTEvent.MOUSE_WHEEL_EVENT_MASK
                                           | AWTEvent.KEY_EVENT_MASK);


    protected Scene scene;

    protected Awtk awtk;


    public Canvas() {
        super();
        this.enableEvents(EVENT_MASK);
        this.setFocusTraversalKeysEnabled(false);

    }


    public synchronized void init(Awtk awtk, Screen screen){

        this.awtk = awtk;

        this.scene = awtk.getScene();

        this.setVisible(true);

        java.awt.Container p = this.getParent();

        BackingStore g = screen.getBackingStore(p,this);

        awtk.init(screen,g);
    }
    public void destroy(){
        this.awtk = null;
        this.scene = null;
    }
    public void start(){
        Awtk awtk = this.awtk;
        if (null != awtk)
            awtk.start();
    }
    public void stop(){
        Awtk awtk = this.awtk;
        if (null != awtk)
            awtk.stop();
    }
    public final Dimension getPreferredSize(){
        java.awt.Container p = this.getParent();
        if (null != p)
            return new Dimension(p.getWidth(),p.getHeight());
        else
            return new Dimension(1,1);
    }
    public final void update(Graphics g){
        this.paint(g);
    }
    public final void paint(Graphics g){
        Awtk awtk = this.awtk;
        if (null != awtk)
            awtk.repaint();
    }
    public final void paintIn(Graphics2D g){
        Scene scene = this.scene;
        if (null != scene)
            scene.paint(g);
        else {
            g.setColor(Color.white);
            g.fillRect(this.getX(),this.getY(),this.getWidth(),this.getHeight());
        }
    }
    public boolean requestFocusInWindow(){
        FocusTP.Instance.install();
        return super.requestFocusInWindow();
    }
    @Override
    protected void processComponentEvent(ComponentEvent event) {
        super.processComponentEvent(event);
        Awtk awtk = this.awtk;
        if (null != awtk)
            awtk.processComponentEvent(event);
    }
    @Override
    protected void processFocusEvent(FocusEvent event) {
        super.processFocusEvent(event);
        Awtk awtk = this.awtk;
        if (null != awtk)
            awtk.processFocusEvent(event);
    }
    @Override
    protected void processMouseEvent(MouseEvent event) {
        super.processMouseEvent(event);
        Awtk awtk = this.awtk;
        if (null != awtk)
            awtk.processMouseEvent(event);
    }
    @Override
    protected void processMouseMotionEvent(MouseEvent event) {
        super.processMouseMotionEvent(event);
        Awtk awtk = this.awtk;
        if (null != awtk)
            awtk.processMouseMotionEvent(event);
    }
    @Override
    protected void processMouseWheelEvent(MouseWheelEvent event) {
        super.processMouseWheelEvent(event);
        Awtk awtk = this.awtk;
        if (null != awtk)
            awtk.processMouseWheelEvent(event);
    }
    @Override
    protected void processKeyEvent(KeyEvent event) {
        super.processKeyEvent(event);
        Awtk awtk = this.awtk;
        if (null != awtk)
            awtk.processKeyEvent(event);
    }

}
